#include "doctorinfopage.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QTextEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>

DoctorInfoPage::DoctorInfoPage(int doctorId, QWidget *parent)
    : QWidget(parent) {
    setupUi(); // 设置初始UI
    loadDoctorInfo(doctorId); // 根据医生ID加载信息
}

void DoctorInfoPage::setupUi() {
    this->isLoading = true;
    QVBoxLayout *layout = new QVBoxLayout(this);

    // 医生头像
    avatarLabel = new QLabel(this);
    QPixmap avatar(":/images/default_avatar.png"); // 默认头像
    avatarLabel->setPixmap(avatar.scaled(100, 100, Qt::KeepAspectRatio));

    // 医生姓名和职称
    nameLabel = new QLabel(this);
    specializationLabel = new QLabel(this);

    // 医生联系方式
    contactLabel = new QLabel(this);

    // 医生简介
    QLabel *descriptionLabel = new QLabel("医生简介: ", this);
    descriptionText = new QTextEdit(this);
    descriptionText->setReadOnly(true); // 设置为只读

    // 创建医生信息组
    QGroupBox *doctorInfoGroup = new QGroupBox("医生信息", this);
    QVBoxLayout *infoLayout = new QVBoxLayout();
    infoLayout->addWidget(nameLabel);
    infoLayout->addWidget(specializationLabel);
    infoLayout->addWidget(contactLabel);
    infoLayout->addWidget(descriptionLabel);
    infoLayout->addWidget(descriptionText);
    doctorInfoGroup->setLayout(infoLayout);

    // 添加头像
    QHBoxLayout *headerLayout = new QHBoxLayout();
    headerLayout->addWidget(avatarLabel);
    headerLayout->addWidget(doctorInfoGroup);

    // 按钮区域
    QPushButton *backButton = new QPushButton("返回", this);
    connect(backButton, &QPushButton::clicked, this, &DoctorInfoPage::close); // 返回按钮功能

    layout->addLayout(headerLayout);
    layout->addWidget(backButton);

    setLayout(layout);
}

void DoctorInfoPage::loadDoctorInfo(int doctorId) {
    tcp = new ClientTcp(DOCTORS_DATA);
    QString sql = QString("SELECT * FROM Doctors WHERE DoctorID = %1").arg(doctorId);
    tcp->sendMsg(sql);

    // 更新UI
    connect(tcp->socket, &QTcpSocket::readyRead, [=]() {
         isLoading = false; // 请求完成，重置状态标志
        QList<QJsonObject> data = tcp->getDoctorsData();

        // 检查是否返回数据
        if (!data.isEmpty()) {
            const QJsonObject &doctorInfo = data.first(); // 获取第一个医生的信息

            // 更新UI组件
            QString name = doctorInfo["Name"].toString();
            QString specialization = doctorInfo["Specialization"].toString();
            QString contactNumber = doctorInfo["ContactNumber"].toString();
            QString introduction = doctorInfo["Introduction"].toString(); // 使用你定义的简介字段
            QString avatarPath = doctorInfo["AvatarPath"].toString(); // 使用头像路径

            nameLabel->setText("姓名: " + name);
            specializationLabel->setText("职称: " + specialization);
            contactLabel->setText("联系方式: " + contactNumber);
            descriptionText->setPlainText(introduction.isEmpty() ? "无简介" : introduction);

            // 更新头像
            QPixmap avatar(avatarPath.isEmpty() ? ":/images/default_avatar.png" : avatarPath);
            avatarLabel->setPixmap(avatar.scaled(100, 100, Qt::KeepAspectRatio));
        } else {
            qDebug() << "没有找到医生信息，DoctorID:" << doctorId;
        }
    });
}
